﻿
using Nest;
using NHibernate.Impl;
using NHibernate.Search.ElasticSearch.Cfg;
using System;
namespace NHibernate.Search.ElasticSearch
{
    public static class SessionExtensions
    {
        public static ISearchResponse<T> Search<T>(this ISession session, Func<QueryContainerDescriptor<T>, QueryContainer> query, int pageIndex, int pageSize) where T : class
        {
            //string index, string type, string term
            NHibernateSearchConfiguration cfg = ConfigurationExtensions.GetNHibernateSearchConfiguration(
                ((SessionFactoryImpl)session.SessionFactory).Name ?? "inovout");

            return cfg.GetElasticClient().Search<T>(s => s
                 .From(pageIndex-1)
                 .Size(pageSize)
                 .Query(query)
             );
        }


        public static ISearchResponse<T> Search<T>(this ISession session, Func<SearchDescriptor<T>, SearchDescriptor<T>> searchSelector) where T : class
        {
            NHibernateSearchConfiguration cfg = ConfigurationExtensions.GetNHibernateSearchConfiguration(
                ((SessionFactoryImpl)session.SessionFactory).Name);
            return cfg.GetElasticClient().Search<T>(searchSelector);
        }
    }
}
